package com.erp.module.jiehao.scJieHao

import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.activity.BaseActivity
import com.erp.myAdapter.ScJieHaoKAdapter
import com.erp.myAdapter.ScJieHaoXuanKAdapter
import com.erp.CaptureActivity
import com.erp.permission.PermissionListener
import com.erp.R
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.util.PermissionUtils
import kotlinx.android.synthetic.main.activity_scjiehao.*
import kotlinx.android.synthetic.main.dialog_xuanjiehao.*
import kotlinx.android.synthetic.main.header_knoya.view.*
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2017/11/17/017.
 * 作者：刘洋
 */
class ScJieHaoKActivity : BaseActivity(), ScJieHaoKView, View.OnClickListener {
    var code=0
    lateinit var jiehaolist :MutableList<String>
    lateinit var scjiehaikp : ScJieHaoKP
    lateinit var adapter : ScJieHaoKAdapter
    lateinit var updialog: AlertDialog
    private val a = "ScJieHaoKActivity"

    override fun getLayoutId(): Int {
        activityname=a;
        return R.layout.activity_scjiehao
    }

    override fun initCreate() {
        code=intent.getIntExtra("code",code)
        jiehaolist=ArrayList()
        scjiehaikp= IScJieHaoKP(this, this)
        scjiehaikp.leixingcode(code)
        adapter= ScJieHaoKAdapter(jiehaolist, this)
        JieHaoLv.adapter=adapter
        ScJieHaoHeader.iv_header_left.visibility=View.VISIBLE
        ScJieHaoHeader.iv_header_left.setOnClickListener(this)
        if(code==1) {
            ScJieHaoHeader.tv_header_title.text = "上传节号"
        }else if(code==2){
            ScJieHaoHeader.tv_header_title.text = "上传纸箱"
        }
        if(code==2){
            JieHaoEt.hint="输入要添加的纸箱"
        }
        LeftBt.setOnClickListener(this)
        RightBt.setOnClickListener(this)
        TianJiaTv.setOnClickListener(this)
        JieHaoLv.onItemLongClickListener = AdapterView.OnItemLongClickListener { _, _, position, _ ->
            val dialog = AlertDialog.Builder(this@ScJieHaoKActivity)
            dialog.setTitle("提示")
            if(code==1) {
                dialog.setMessage("确定要删除这个节号吗？")
            }else if(code==2){
                dialog.setMessage("确定要删除这个纸箱吗？")
            }
            dialog.setPositiveButton("确定") { _, _ ->
                jiehaolist.removeAt(position)
                adapter.notifyDataSetChanged()
            }
            dialog.setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
            dialog.show()
            true
        }
        ScJieHaoHeader.tv_header_title.isFocusable = true
        ScJieHaoHeader.tv_header_title.isFocusableInTouchMode = true
        ScJieHaoHeader.tv_header_title.requestFocus()
        ScJieHaoHeader.tv_header_title.requestFocusFromTouch()
    }
    override fun addJieHaoOK(data: String) {
        updialog.dismiss()
        var intent=Intent()
        intent.putExtra("isadd",true)
        setResult(Activity.RESULT_OK,intent)
        finish()
    }

    override fun addJieHaoNo(erroe: String) {
        updialog.dismiss()
    }
    override fun onClick(v: View?) {
        when(v){
            LeftBt -> {
                PermissionUtils.issaomiao(this@ScJieHaoKActivity,object : PermissionListener {
                    override fun onSucceed(requestCode: Int, grantPermissions: MutableList<String>) {
                        val intent = Intent(this@ScJieHaoKActivity, CaptureActivity::class.java)
                        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                        startActivityForResult(intent, 1)
                    }

                    override fun onFailed(requestCode: Int, deniedPermissions: MutableList<String>) {
                        Myutil.MyTost("没有权限，无法扫描")
                    }

                })
            }
            RightBt -> {
                if(jiehaolist.size>0) {
                    updialog = Myutil.showupdatedialog(this, "正在上传", false)
                    scjiehaikp.addJieHao(jiehaolist)
                }else{
                    if(code==1) {
                        Myutil.MyTost("请先添加要上传的节号")
                    }else if(code==2){
                        Myutil.MyTost("请先添加要上传的纸箱")
                    }
                }
            }
            ScJieHaoHeader.iv_header_left -> {
                finish()
            }
            TianJiaTv -> {
                if(JieHaoEt.text.toString().trim()!="") {
                    jiehaolist.add(JieHaoEt.text.toString())
                    adapter.notifyDataSetChanged()
                    Myutil.notjianpan(this@ScJieHaoKActivity,ScJieHaoHeader.iv_header_left)
                    JieHaoEt.setText("")
                }else{
                    if(code==1) {
                        Myutil.MyTost("请输入要添加的节号")
                    }else if(code==2){
                        Myutil.MyTost("请输入要添加的纸箱")
                    }
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null||resultCode!= Activity.RESULT_OK){
            return
        }
        when(requestCode){
            1 -> {
                var jh : MutableList<String> =ArrayList()
                val bundle = data.extras
                val s = bundle?.get("result") as String
                try {
                    val jo=JSONObject(s)
                    val js=JsonUtil.getjsvolue(jo,"b")
                    for(i in 0..js.length()){
                        jh.add(js[i].toString())
                    }
                }catch (e : JSONException){
                    e.printStackTrace()
                }
                Log.d("TAAAG","jh="+jh)
                if(jh.size==1){
                    adapter.notifyDataSetChanged()
                    addjiehao(jh[0])
                }else if(jh.size>1){
                    XuanJieHaoDialog(jh)
                }
            }
        }
    }
    private fun addjiehao(s :String){
        for(ss in jiehaolist){
            if(s==ss){
                if(code==1) {
                    Myutil.MyTost("本节号已经存在，请不要重新扫描")
                }else if(code==2){
                    Myutil.MyTost("本纸箱已经存在，请不要重新扫描")
                }
                return
            }
        }
        jiehaolist.add(s)
    }
    private fun XuanJieHaoDialog(list :MutableList<String>){
        val dialog= Dialog(this,R.style.MyDialog)
        dialog.show()
        dialog.setContentView(R.layout.dialog_xuanjiehao)
        val adapterdialog= ScJieHaoXuanKAdapter(list, this)
        dialog.XJieHaoLv.adapter=adapterdialog
        dialog.XJieHaoLv.setOnItemClickListener { parent, view, position, id ->
            adapterdialog.dowenitem(position)
            adapterdialog.notifyDataSetChanged()
        }
        dialog.btQueding.setOnClickListener {
            addjiehaos(adapterdialog.list)
            adapter.notifyDataSetChanged()
            dialog.dismiss()
        }
    }
    fun addjiehaos(list :MutableList<String>){
        for(s in list){
            if(!jiehaolist.contains(s)){
                jiehaolist.add(s)
            }
        }
    }
}